home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / base / async.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  9.3 KB  |  375 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''
  5. Basic infrastructure for asynchronous socket service clients and servers.
  6.  
  7. There are only two ways to have a program on a single processor do "more
  8. than one thing at a time".  Multi-threaded programming is the simplest and
  9. most popular way to do it, but there is another very different technique,
  10. that lets you have nearly all the advantages of multi-threading, without
  11. actually using multiple threads. it\'s really only practical if your program
  12. is largely I/O bound. If your program is CPU bound, then pre-emptive
  13. scheduled threads are probably what you really need. Network servers are
  14. rarely CPU-bound, however.
  15.  
  16. If your operating system supports the select() system call in its I/O
  17. library (and nearly all do), then you can use it to juggle multiple
  18. communication channels at once; doing other work while your I/O is taking
  19. place in the "background."  Although this strategy can seem strange and
  20. complex, especially at first, it is in many ways easier to understand and
  21. control than multi-threaded programming. The module documented here solves
  22. many of the difficult problems for you, making the task of building
  23. sophisticated high-performance network servers and clients a snap.
  24.  
  25. NOTICE: This copy of asyncore has been modified from the Python Std Lib version.
  26.  
  27. '''
  28. from g import *
  29. from codes import *
  30. import select
  31. import socket
  32. import sys
  33. import time
  34. import os
  35. import thread
  36. import fcntl
  37. from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, ENOTCONN, ESHUTDOWN, EINTR, EISCONN
  38. socket_map = { }
  39.  
  40. class ExitNow(Exception):
  41.     pass
  42.  
  43.  
  44. def loop(timeout = 1, sleep_time = 0.1):
  45.     while socket_map:
  46.         r = []
  47.         w = []
  48.         e = []
  49.         for fd, obj in socket_map.items():
  50.             if obj.readable():
  51.                 r.append(fd)
  52.             
  53.             if obj.writable():
  54.                 w.append(fd)
  55.                 continue
  56.         
  57.         if r == r and w == w:
  58.             pass
  59.         elif w == e:
  60.             time.sleep(timeout)
  61.         else:
  62.             
  63.             try:
  64.                 (r, w, e) = select.select(r, w, e, timeout)
  65.             except select.error:
  66.                 []
  67.                 err = []
  68.                 if err[0] != EINTR:
  69.                     raise Error(ERROR_INTERNAL)
  70.                 
  71.                 r = []
  72.                 w = []
  73.                 e = []
  74.             except:
  75.                 r
  76.  
  77.         for fd in r:
  78.             
  79.             try:
  80.                 obj = socket_map[fd]
  81.             except KeyError:
  82.                 []
  83.                 []
  84.                 continue
  85.             except:
  86.                 r
  87.  
  88.             
  89.             try:
  90.                 obj.handle_read_event()
  91.             continue
  92.             except ExitNow:
  93.                 []
  94.                 []
  95.                 raise ExitNow
  96.                 continue
  97.                 except Error:
  98.                     e = None
  99.                     obj.handle_error(e)
  100.                     continue
  101.                 
  102.             for fd in w:
  103.                 
  104.                 try:
  105.                     obj = socket_map[fd]
  106.                 except KeyError:
  107.                     r<EXCEPTION MATCH>ExitNow
  108.                     r<EXCEPTION MATCH>ExitNow
  109.                     []
  110.                     continue
  111.                 except:
  112.                     []
  113.  
  114.                 
  115.                 try:
  116.                     obj.handle_write_event()
  117.                 except ExitNow:
  118.                     []
  119.                     []
  120.                     raise ExitNow
  121.                 except Error:
  122.                     e = None
  123.                     obj.handle_error(e)
  124.                 except:
  125.                     []
  126.  
  127.                 time.sleep(sleep_time)
  128.             
  129.  
  130.         continue
  131.         []
  132.  
  133.  
  134. class dispatcher:
  135.     connected = False
  136.     accepting = False
  137.     closing = False
  138.     addr = None
  139.     
  140.     def __init__(self, sock = None):
  141.         if sock:
  142.             self.set_socket(sock)
  143.             self.socket.setblocking(0)
  144.             self.connected = True
  145.             
  146.             try:
  147.                 self.addr = sock.getpeername()
  148.             except socket.error:
  149.                 pass
  150.             except:
  151.                 None<EXCEPTION MATCH>socket.error
  152.             
  153.  
  154.         None<EXCEPTION MATCH>socket.error
  155.         self.socket = None
  156.  
  157.     
  158.     def __repr__(self):
  159.         status = [
  160.             self.__class__.__module__ + '.' + self.__class__.__name__]
  161.         if self.accepting and self.addr:
  162.             status.append('listening')
  163.         elif self.connected:
  164.             status.append('connected')
  165.         
  166.         if self.addr is not None:
  167.             
  168.             try:
  169.                 status.append('%s:%d' % self.addr)
  170.             except TypeError:
  171.                 status.append(repr(self.addr))
  172.             except:
  173.                 None<EXCEPTION MATCH>TypeError
  174.             
  175.  
  176.         None<EXCEPTION MATCH>TypeError
  177.         return '<%s at %#x>' % (' '.join(status), id(self))
  178.  
  179.     
  180.     def add_channel(self):
  181.         socket_map[self._fileno] = self
  182.  
  183.     
  184.     def del_channel(self):
  185.         fd = self._fileno
  186.         if socket_map.has_key(fd):
  187.             del socket_map[fd]
  188.         
  189.  
  190.     
  191.     def create_socket(self, family, type):
  192.         self.family_and_type = (family, type)
  193.         self.socket = socket.socket(family, type)
  194.         self.socket.setblocking(0)
  195.         self._fileno = self.socket.fileno()
  196.         self.add_channel()
  197.  
  198.     
  199.     def set_socket(self, sock):
  200.         self.socket = sock
  201.         self._fileno = sock.fileno()
  202.         self.add_channel()
  203.  
  204.     
  205.     def set_reuse_addr(self):
  206.         
  207.         try:
  208.             self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) | 1)
  209.         except socket.error:
  210.             pass
  211.  
  212.  
  213.     
  214.     def readable(self):
  215.         return True
  216.  
  217.     
  218.     def writable(self):
  219.         return True
  220.  
  221.     
  222.     def listen(self, num):
  223.         self.accepting = True
  224.         return self.socket.listen(num)
  225.  
  226.     
  227.     def bind(self, addr):
  228.         self.addr = addr
  229.         return self.socket.bind(addr)
  230.  
  231.     
  232.     def connect(self, address):
  233.         self.connected = False
  234.         err = self.socket.connect_ex(address)
  235.         if err in (EINPROGRESS, EALREADY, EWOULDBLOCK):
  236.             return None
  237.         
  238.         if err in (0, EISCONN):
  239.             self.addr = address
  240.             self.connected = True
  241.             self.handle_connect()
  242.         else:
  243.             raise socket.error, err
  244.  
  245.     
  246.     def accept(self):
  247.         
  248.         try:
  249.             (conn, addr) = self.socket.accept()
  250.             return (conn, addr)
  251.         except socket.error:
  252.             why = None
  253.             if why[0] == EWOULDBLOCK:
  254.                 pass
  255.             else:
  256.                 raise socket.error, why
  257.         except:
  258.             why[0] == EWOULDBLOCK
  259.  
  260.  
  261.     
  262.     def send(self, data):
  263.         
  264.         try:
  265.             result = self.socket.send(data)
  266.             return result
  267.         except socket.error:
  268.             why = None
  269.             if why[0] == EWOULDBLOCK:
  270.                 return 0
  271.             else:
  272.                 raise socket.error, why
  273.             return 0
  274.  
  275.  
  276.     
  277.     def recv(self, buffer_size):
  278.         
  279.         try:
  280.             data = self.socket.recv(buffer_size)
  281.             if not data:
  282.                 self.handle_close()
  283.                 return ''
  284.             else:
  285.                 return data
  286.         except socket.error:
  287.             why = None
  288.             if why[0] in [
  289.                 ECONNRESET,
  290.                 ENOTCONN,
  291.                 ESHUTDOWN]:
  292.                 self.handle_close()
  293.                 return ''
  294.             else:
  295.                 raise socket.error, why
  296.         except:
  297.             why[0] in [
  298.                 ECONNRESET,
  299.                 ENOTCONN,
  300.                 ESHUTDOWN]
  301.  
  302.  
  303.     
  304.     def close(self):
  305.         self.del_channel()
  306.         self.socket.close()
  307.  
  308.     
  309.     def __getattr__(self, attr):
  310.         return getattr(self.socket, attr)
  311.  
  312.     
  313.     def handle_read_event(self):
  314.         if self.accepting:
  315.             if not self.connected:
  316.                 self.connected = True
  317.             
  318.             self.handle_accept()
  319.         elif not self.connected:
  320.             self.handle_connect()
  321.             self.connected = True
  322.             self.handle_read()
  323.         else:
  324.             self.handle_read()
  325.  
  326.     
  327.     def handle_write_event(self):
  328.         if not self.connected:
  329.             self.handle_connect()
  330.             self.connected = True
  331.         
  332.         self.handle_write()
  333.  
  334.     
  335.     def handle_expt_event(self):
  336.         self.handle_expt()
  337.  
  338.     
  339.     def handle_error(self, e):
  340.         log.error('Error processing request.')
  341.         raise Error(ERROR_INTERNAL)
  342.  
  343.     
  344.     def handle_expt(self):
  345.         raise Error
  346.  
  347.     
  348.     def handle_read(self):
  349.         raise Error
  350.  
  351.     
  352.     def handle_write(self):
  353.         raise Error
  354.  
  355.     
  356.     def handle_connect(self):
  357.         raise Error
  358.  
  359.     
  360.     def handle_accept(self):
  361.         raise Error
  362.  
  363.     
  364.     def handle_close(self):
  365.         self.close()
  366.  
  367.  
  368.  
  369. def close_all():
  370.     for x in channels.values():
  371.         x.channels.close()
  372.     
  373.     channels.clear()
  374.  
  375.